package cn.kimming.wms.web.action;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import cn.kimming.wms.anno.RequiredPermission;
import cn.kimming.wms.domain.JsonMsg;
import cn.kimming.wms.domain.SystemMenu;
import cn.kimming.wms.excep.ObjectIsUsedException;
import cn.kimming.wms.query.SystemMenuQueryObject;
import cn.kimming.wms.service.ISystemMenuService;
import lombok.Getter;

@Controller("systemMenuAction")
@Scope("prototype")
public class SystemMenuAction extends BaseAction {

	private static final long serialVersionUID = -9035684507629635540L;

	@Autowired
	private ISystemMenuService systemMenuService;
	
	@Getter
	private SystemMenu systemMenu = new SystemMenu();
	
	@Getter 
	private SystemMenuQueryObject qo = new SystemMenuQueryObject();

	
	@Override
	@RequiredPermission("菜单列表")
	@InputConfig(methodName="input")
	public String execute() {
		try {
			ActionContext.getContext().put("menus", systemMenuService.query(qo));
			ActionContext.getContext().put("parents", systemMenuService.listParents(qo.getParentId()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LIST;
	}
	
	@RequiredPermission("菜单编辑")
	public String input() {
		try {
			if (systemMenu.getId() != null) {
				systemMenu = systemMenuService.get(systemMenu.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return INPUT;
	}

	@RequiredPermission("菜单保存和更新")
	public String saveOrUpdate() {
		
		if (systemMenu.getId() != null) {
			try {
				systemMenuService.update(systemMenu);
				addActionMessage("更新成功!");
			} catch(Exception e) {
				e.printStackTrace();
				addActionError("系统异常,更新失败!");
			}
		} else {
			try {
				systemMenuService.save(systemMenu);
				addActionMessage("保存成功!");
			} catch(Exception e) {
				e.printStackTrace();
				addActionError("系统异常,保存失败!");
			}
		}
		return SUCCESS;
	}

	@RequiredPermission("菜单删除")
	public String delete() {
		try {
			if (systemMenu.getId() != null) {
				systemMenuService.delete(systemMenu.getId());
				putJSON(new JsonMsg(true, "删除成功"));
			}
		} catch(ObjectIsUsedException e) {
			putJSON(new JsonMsg(false, e.getMessage()));
		} catch (Exception e) {
			e.printStackTrace();
			putJSON(new JsonMsg(false, "系统异常,删除失败!"));
		}
		return NONE;
	}
	
	//加载根菜单下的子菜单
	public String loadMenus() {
		String parentSn = qo.getParentSn();
		if (StringUtils.isNotEmpty(parentSn)) {
			try {
				putJSON(systemMenuService.loadMenus(parentSn));
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
		return NONE;
	}

	public void prepareSaveOrUpdate() throws Exception {
		if (systemMenu.getId() != null) {
			systemMenu = systemMenuService.get(systemMenu.getId());
		}
		if (qo.getParentId() > 0) {
			systemMenu.setParent(systemMenuService.get(qo.getParentId()));
		}
	}
	
	public String getParentName() {
		if (qo.getParentId() > 0) {
			return systemMenuService.get(qo.getParentId()).getName();
		}
		return "根菜单";
	}
	
}
